<?php

namespace app\lib;

use app\models\OperationLog;
use yii;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use app\models\Baoxiao;

class BaseFunction
{
    public static function check_excel($type)
    {
        $array = [
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'application/vnd.ms-excel'
        ];
        if (in_array($type, $array)) {
            return 1;
        }
        return 0;
    }

    public static function check_img($input_type)
    {
        $type = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp'];
        if (in_array($input_type, $type)) {
            return 1;
        } else {
            return 0;
        }
    }

    //获取excel对象
    public static function get_excel($file_path, $ext)
    {
        if ($ext == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file_path, 'utf-8');
        } elseif ($ext == 'xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_path, 'utf-8');
        }

        return $objPHPExcel;
    }

    //检测业务员编辑权限
    public static function check_edit_auth($role)
    {
        if (!($role == 1 || $role == 2)) {
            echo json_encode(['code' => 0, 'msg' => '对不起，暂无权限']);
            exit;
        }
    }

    /**
     * 生成jwt token
     * @param int $id id
     * @param string $zh_name 中文名
     * @return string
     */
    public function generate_jwt($id, $zh_name)
    {
        $builder = new Builder();
        $token = $builder->set('id', $id)
            ->set('zh_name', $zh_name)
            ->set('expire_time', time() + 86400)
            ->sign((new Sha256()), yii::$app->params['jwt_token'])
            ->getToken();

        return (string)$token;
    }

    /**
     * 前后端分离生成jwt
     * @param int $id id
     * @param string $zh_name 中文名
     * @return string
     */
    public function generateJwt($user)
    {
        $builder = new Builder();
        $token = $builder->set('id', $user['id'])
            ->set('zh_name', $user['name'])
            ->set('role', $user['role'])
            ->set('rules', $user['new_rules'])
            ->set('expire_time', time() + 86400)
            ->sign((new Sha256()), yii::$app->params['jwt_token'])
            ->getToken();

        return (string)$token;
    }

    /**
     * jwt校验
     * @param  string $token
     * @return bool
     */
    public function verify_jwt($jwt)
    {
        $token = (new Parser())->parse($jwt);
        return $token->verify((new Sha256()), yii::$app->params['jwt_token']);
    }

    public static function check_baoxiao()
    {
        $model = new Baoxiao();
        $info = $model->check_baoxiao();
        if ($info && $info['num'] > 0) {
            return 1;
        }
        return 0;
    }

    /**上传文件
     * @param $file
     * @return bool|string
     */
    public static function upload($file)
    {
        $suffix = explode('.', $file['name']);
        if (!is_dir(ROOT_PATH . '/upload/' . date('Y-m-d'))) {
            mkdir(ROOT_PATH . '/upload/' . date('Y-m-d'), 0766);
        }
        $path = '/upload/' . date('Y-m-d') . '/' . substr(md5(time() . $file['name']), 0, 16) . '.' . $suffix[count($suffix) - 1];
        $re = move_uploaded_file($file['tmp_name'], ROOT_PATH . $path);
        if ($re) {
            return $path;
        }
        return false;
    }

    /**
     * 操作日志记录
     */
    public static function operation($array)
    {
        $model = new OperationLog();
        $model->insert($array);
    }

    public static function checkJieke($role)
    {
        if ($role == 9 || $role == 10) {
            return true;
        }
        return false;
    }

    public static function profit($v, $lilun, $staffList)
    {
        $saleman_get = 0;
        $purchase_get = 0;
        if ($v['saleman_num'] == 1 || $v['saleman_num'] == 2) {
            //大客户组暂时不进行计算
        } else {
            foreach ($staffList as $vaa) {
                //获取采购入职时间
                if ($vaa['job_number'] == $v['saleman_num']) {
                    if (strtotime($vaa['in_date']) > strtotime('2017-01-01')) {
                        //2017入职
                        $saleman_get = $lilun * 0.15;
                        $purchase_get = 0;
                        if ($v['saleman_num'] != $v['purchase'] && $v['purchase'] > 0) {
                            if (strtotime($v['date']) >= strtotime('2017-05')) {
                                $purchase_get = $lilun * 0.05;
                            }

                        }
                    } else {
                        if (strtotime($v['date']) <= strtotime('2017-05')) {
                            //5月份以前
                            $saleman_get = $lilun * 0.2;
                            $purchase_get = 0;
                        } else {
                            //5月份以后
                            if ($v['saleman_num'] == $v['purchase']) {
                                //业务采购相同20%
                                $saleman_get = $lilun * 0.2;
                                $purchase_get = 0;
                            } else {
                                $saleman_get = $lilun * 0.15;
                                $purchase_get = $lilun * 0.05;
                            }
                        }
                    }

                    break;
                }
            }
            $saleman_get = round($saleman_get, 2);
            $purchase_get = $purchase_get ? round($purchase_get, 2) : 0;
            return [$saleman_get, $purchase_get];
        }
    }

}
